﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Windows.System;
using 高级控件演示.Domain;

namespace 高级控件演示.ViewModels.Dialogs
{
    public class PlayPigeonDialogViewModel : BindableBase, IDialogAware
    {

        private DispatcherTimer _timer;
        private int _currentIndex;

        #region 依赖属性

        private string pigeonOwner;

        public string PigeonOwner
        {
            get { return pigeonOwner; }
            set { pigeonOwner = value; RaisePropertyChanged(); }
        }

        private string displayText;

        public string DisplayText
        {
            get { return displayText; }
            set { displayText = value;RaisePropertyChanged(); }
        }

        private PigeonInfo pigeonInfo;
        public PigeonInfo PigeonInfo
        {
            get { return pigeonInfo; }
            set { pigeonInfo = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 命令
        public DelegateCommand OkCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }
        public DelegateCommand CheckAllCommand { get; private set; }
        public DelegateCommand ClearCommand { get; private set; }
        public DelegateCommand CloseCommand { get; private set; }
        public DelegateCommand StartTypingCommand { get; private set; }
        #endregion

        public PlayPigeonDialogViewModel()
        {
            CloseCommand = new DelegateCommand(CloseCommandFunc);
            StartTypingCommand = new DelegateCommand(StartTypingCommandFunc);

            
            _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
            _timer.Tick += (s, e) => TypeNextCharacter();
            StartTypingCommandFunc();
        }

        private void CloseCommandFunc()
        {
            RequestClose.Invoke(null, ButtonResult.Cancel);
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            //CloseWithUserData();
        }


        public void OnDialogOpened(IDialogParameters parameters)
        {
            PigeonInfo = parameters["PigeonInfo"] as PigeonInfo;

                            
        }

        public DialogCloseListener RequestClose { get; }


        private void StartTypingCommandFunc()
        {
            DisplayText = "";
            _currentIndex = 0;
            _timer.Start();
        }

        private void TypeNextCharacter()
        {
            if (_currentIndex < PigeonInfo.Desc.Length)
            {
                DisplayText += PigeonInfo.Desc[_currentIndex];
                _currentIndex++;
            }
            else
            {
                _timer.Stop();
            }
        }
    }
}
